package org.example.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.example.searcher.DocSearcher;
import org.example.searcher.Result;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
public class DocSearcherController {

    public static DocSearcher docSearcher = new DocSearcher();
    public ObjectMapper objectMapper = new ObjectMapper();

    @RequestMapping(value = "/searcher", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String search(@RequestParam("query") String query) {
        log.info("查询开启：" + query);
        List<Result> results = docSearcher.search(query);
        try {
            return objectMapper.writeValueAsString(results);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
